package com.simen.hellobytes.lang;

/**
 * author: SimenHi
 * create time: 2019-12-31 16:22
 * description:
 */
public class TypeParseUtil {

    public static float parseFloat(String floatStr) {
        return parseFloat(floatStr, 0f);
    }

    public static float parseFloat(String floatStr, float def) {
        if (floatStr != null) {
            try {
                return Float.parseFloat(floatStr);
            } catch (NumberFormatException e) {
            }
        }

        return def;
    }

    public static double parseDouble(String doubleStr) {
        return parseDouble(doubleStr, 0);
    }

    public static double parseDouble(String doubleStr, double def) {
        if (doubleStr != null) {
            try {
                return Double.parseDouble(doubleStr);
            } catch (NumberFormatException e) {
            }
        }

        return def;
    }

    public static short parseShort(String shortStr) {
        return parseShort(shortStr, (short) 0);
    }

    public static short parseShort(String shortStr, short def) {
        if (shortStr != null) {
            try {
                return Short.parseShort(shortStr);
            } catch (NumberFormatException e) {
            }
        }

        return def;
    }

    public static int parseInt(String intStr) {
        return parseInt(intStr, 0);
    }

    public static int parseInt(String intStr, int def) {
        if (intStr != null) {
            try {
                return Integer.parseInt(intStr);
            } catch (NumberFormatException e) {
            }
        }

        return def;
    }

    public static long parseLong(String longStr) {
        return parseLong(longStr, 0);
    }

    public static long parseLong(String longStr, long def) {
        if (longStr != null) {
            try {
                return Long.parseLong(longStr);
            } catch (NumberFormatException e) {
            }
        }

        return def;
    }

    public static boolean parseBoolean(String booleanStr) {
        return parseBoolean(booleanStr, false);
    }

    public static boolean parseBoolean(String booleanStr, boolean def) {
        if (booleanStr != null) {
            try {
                return Boolean.parseBoolean(booleanStr);
            } catch (NumberFormatException e) {
            }
        }

        return def;
    }

}
